/* when the page has finished loading.. execute the follow */
$(document).ready(function () {
    jQuery("#personList").jqGrid({
        url:'/person/listPeopleAsJSON?companyId=' + companyId + '&type=' + type,
        datatype: "json",
        colNames:['Title', 'First Name', 'Last Name', 'Phone', 'Email', 'Notes', 'id'],
        colModel:[
            {name:'title', width:75},
            {name:'firstName', width:95},
            {name:'lastName', width:95},
            {name:'phone', width:144},
            {name:'email', width:150},
            {name:'notes', width:500},
            {name:'id', hidden:true}
        ],
        altRows: true,
        autowidth: true,
        height: 332,
        message: 'The data is not available. Please refresh the screen.',
        pager: '#personListPager',
        rowList: [20, 50, 75, 100],
        rowNum: 20,
//        scroll: true,
        shrinkToFit: false,
        subGrid: false,
        viewrecords: true,
        onSelectRow: function(id) {
            window.location = "/person/edit/" + id + "?type=" + type
        }
    });
    jQuery("#personList").jqGrid('gridResize',{minWidth:850,minHeight:80, maxHeight:600});
});

var timeoutHnd;
var type;

function doSearch(ev) {
    if (timeoutHnd) {
        clearTimeout(timeoutHnd)
    }
    timeoutHnd = setTimeout(gridReload,500)
}

function gridReload() {
    var searchValue = jQuery("#personSearch").val();
    jQuery("#personList").jqGrid('setGridParam', {url:"listPeopleAsJSON?searchValue=" + searchValue, page:1}).trigger("reloadGrid");
}
